﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Framework;
using CNVP.Config;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class AdminEdit : AdminPage
    {
        BLL.CNVP_Admin bll = new BLL.CNVP_Admin();
        Model.CNVP_Admin model = new Model.CNVP_Admin();
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 85)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ddlRoles();
                if (id > 0)
                {
                    txtAdminName.CssClass = "Display";
                    lbTitle.Text = " > 修改";
                    DataInfo(id);
                }
                else 
                {
                    int AdminRoleID = 0;
                    if (!string.IsNullOrEmpty(Request.QueryString["AdminRoleID"]) && Public.IsNumber(Request.QueryString["AdminRoleID"]))
                    {
                        AdminRoleID = Convert.ToInt32(Request.QueryString["AdminRoleID"]);
                    }
                    if (AdminRoleID > 0)
                    {
                        ddlRoleID.SelectedValue = AdminRoleID.ToString();
                    }
                    LastLoginTime.Visible = false;
                    LastLoginIp.Visible = false;
                    LoginNum.Visible = false;
                    lbTitle.Text = " > 添加";
                    txtAdminName.Attributes.Add("onchange", "IsAdminName();");
                }
            }
        }

        private void ddlRoles()
        {
            IList<Model.CNVP_AdminRole> model = new BLL.CNVP_AdminRole().GetAll_CNVP_AdminRole("IsLock=0");
            foreach (Model.CNVP_AdminRole c in model)
            {
                ddlRoleID.Items.Add(new ListItem(c.RoleName, c.ID.ToString()));
            }
        }

        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_AdminById(id);
            if (model != null)
            {
                lbtxtAdminName.Text = model.AdminName;
                txtAdminName.Text = model.AdminName;
                rblIsLock.SelectedValue = model.IsLock.ToString();
                rblIsAdmin.SelectedValue = model.IsAdmin.ToString();
                txtAdminText.Text = model.AdminText;
                txtDepartmentName.Text = model.DepartmentName;
                txtAdminNo.Text = model.AdminNo;
                ddlRoleID.SelectedValue = model.RoleID.ToString();
                lbLastLoginTime.Text = model.LastLoginTime.ToString();
                lbLastLoginIp.Text = model.LastLoginIp;
                lbLoginNum.Text = model.LoginNum.ToString();
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            
            if (id > 0)
            {
                int rows = new BLL.CNVP_Admin().Update_CNVP_Admin(new Model.CNVP_Admin()
                {
                    ID = id,
                    AdminName = txtAdminName.Text.Trim(),
                    IsLock = byte.Parse(rblIsLock.SelectedValue),
                    IsAdmin = byte.Parse(rblIsAdmin.SelectedValue),
                    DepartmentName = txtDepartmentName.Text,
                    AdminNo = txtAdminNo.Text,
                    AdminText = txtAdminText.Text.Trim(),
                    RoleID = int.Parse(ddlRoleID.SelectedValue),
                    AdminPass = EncryptUtils.MD5(txtPwd1.Text.Trim())
                    
                });
                if (rows == 1)
                {
                    BasePage.MessageBox("修改成功！", "AdminList.aspx?Page=" + Request.QueryString["Page"] + "");
                }
                else
                {
                    MessageUtils.ShowPre("修改失败！"); 
                }
            }
            else
            {
                if (txtAdminName.Text.Trim() == "")
                {
                    MessageUtils.ShowPre("请输入管理员账户！");
                    return;
                }
                var AdminsList = new BLL.CNVP_Admin().QurryListByAdminName(this.txtAdminName.Text.Trim());
                if (AdminsList.Count > 0)
                {
                    MessageUtils.ShowPre("管理员账户已经存在，请重新输入！");
                    return;
                }
                else
                {
                    int rows = new BLL.CNVP_Admin().Add_CNVP_Admin(new Model.CNVP_Admin()
                    {
                        AdminName = txtAdminName.Text.Trim(),
                        AdminPass = EncryptUtils.MD5(txtPwd1.Text.Trim()),
                        IsLock = byte.Parse(rblIsLock.SelectedValue),
                        IsAdmin = byte.Parse(rblIsAdmin.SelectedValue),
                        DepartmentName = txtDepartmentName.Text,
                        AdminNo = txtAdminNo.Text,
                        AdminText = txtAdminText.Text.Trim(),
                        RoleID = int.Parse(ddlRoleID.SelectedValue),
                        LoginNum = 0

                    });
                    if (rows == 1)
                    {
                        BasePage.MessageBox("添加成功！", "AdminList.aspx");
                    }
                    else
                    {
                        MessageUtils.ShowPre("添加失败！");
                    }
                }
            }
        }

    }
}
